package com.example.lists.LazyListState

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material.FloatingActionButton
import androidx.compose.material.Icon
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Favorite
import androidx.compose.runtime.Composable
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch


//对于简单的使用情况，应用程序通常只需要知道第一个可见的 item 的信息。为此，LazyListState 提供了 firstVisibleItemIndex 和 firstVisibleItemScrollOffset 属性。
@Composable
fun LazyListStateDemo() {
    Box {
        val listState = rememberLazyListState()
        val scope = rememberCoroutineScope()

        StickyHeadersSample(listState = listState , scope = scope)

        // 添加一个用于是否显示按钮的代码
        // 如果第一个可见的项目已经被移动过去，就显示这个按钮。
        val showButton = remember {
            derivedStateOf {  // 尽量减少不必要的合成
                listState.firstVisibleItemIndex > 0
            }
        }
        /*Box(
            contentAlignment = Alignment.BottomEnd
        ) {
            AnimatedVisibility(visible = showButton.value) {
                ScrollToTopButton(scope = scope, listState = listState)
            }
        }*/

    }
}

/*
@Composable
fun ScrollToTopButton(
    scope: CoroutineScope,
    listState: LazyListState
) {
    FloatingActionButton(
        onClick = {
            scope.launch {
                listState.animateScrollToItem(0) //返回第一项
            }
        }
    ) {
        Icon(Icons.Filled.Favorite, contentDescription = null)
    }
}*/
